﻿using voidbox;
using System.Numerics;
using TTEngine.Graphics;
using TTEngine.Resource;
using TTEngine.TTMath;

namespace TTEngine.Framework.UI
{
    public class UILabel : UIBaseElement
    {
        private IFont _font;
        public string Text;
        public bool _withShadow = true;
        public bool _withBorder = false;
        public Color _colorShadow = new Color(0, 0, 0, 0.5f);
        public Vector2 _fontScale = new Vector2(1.0f, 1.0f);
        public float _fontBorder = 1;
        public bool Cut = false;
        public UIVAlign VAlign = UIVAlign.Middle;
        public UIHAlign HAlign = UIHAlign.Middle;
        public int space = 1;
        private Vector2 _pos = new Vector2(0, 0);
        public IFont Font
        {
            get { return _font; }
            set
            {
                _font = value;
                float fs = 1.0f;
                if (_font.FontSize > 16)
                    fs = 16.0f / _font.FontSize;
                _fontBorder = 1 / fs;
                _fontScale = new Vector2(fs, fs);
            }
        }
        public UILabel()
        {
            //Font = font;
            VAlign = UIVAlign.Middle;
            HAlign = UIHAlign.Middle;
            Text = "Label";
            LocalRect.SetByRect(new Rectangle(0, 0, 100, 16));
        }


        public float GetTextWidth()
        {
            if (_font == null)
                return 0;
            float width = 0;
            var change = _font.EnsureTextPacked(Text);
            foreach (var c in Text)
            {
                if (_font.TryGetCharInfo(c, out var info))
                {
                    width += info.fontadv;
                    width += space;
                }
            }
            if (change)
            {
                _font.ApplyPackedTextureData();
            }
            return width * _fontScale.X;
        }

        public override void OnRender(UICanvas canvas)
        {
            if (_font != null)
            {
                var sw = GetWorldRect();
                float fontHeight = _font.FontSize * _fontScale.Y;
                float fontWidth = GetTextWidth();

                if (HAlign == UIHAlign.Left)
                {
                    _pos.X = sw.x1;
                }
                else if (HAlign == UIHAlign.Middle)
                {
                    _pos.X = sw.x1 + (sw.Width - fontWidth) / 2;
                }
                else if (HAlign == UIHAlign.Right)
                {
                    _pos.X = sw.x1 + (sw.Width - fontWidth);
                }

                if (VAlign == UIVAlign.Top)
                {
                    _pos.Y = sw.y1;
                }
                else if (VAlign == UIVAlign.Middle)
                {
                    _pos.Y = sw.y1 + (sw.Height - fontHeight) / 2;
                }
                else if (VAlign == UIVAlign.Bottom)
                {
                    _pos.Y = sw.y1 + (sw.Height - fontHeight);
                }

                float fontBorder = _fontBorder * _fontScale.Y;
                var outs = new Vector2(_fontScale.X, _fontScale.Y);

                if (Cut)
                {
                    //推裁剪
                    canvas.Batcher.EndDraw();
                    canvas.Batcher.PushLimitRect(GetWorldRectScale(canvas._camera.Scale));
                }

                {
                    if (_withBorder)
                    {
                        canvas.Batcher.GetPackedTextureBatcher().DrawFont(_font, Text, _pos.X + fontBorder * -1, _pos.Y + 0, 0, _colorShadow, _fontScale.Y, space);
                        canvas.Batcher.GetPackedTextureBatcher().DrawFont(_font, Text, _pos.X + fontBorder * 1, _pos.Y + 0, 0, _colorShadow, _fontScale.Y, space);
                        canvas.Batcher.GetPackedTextureBatcher().DrawFont(_font, Text, _pos.X + 0, _pos.Y + fontBorder * -1, 0, _colorShadow, _fontScale.Y, space);
                        canvas.Batcher.GetPackedTextureBatcher().DrawFont(_font, Text, _pos.X + 0, _pos.Y + fontBorder * 1, 0, _colorShadow, _fontScale.Y, space);

                    }
                    else if (_withShadow)
                    {
                        canvas.Batcher.GetPackedTextureBatcher().DrawFont(_font, Text, _pos.X + fontBorder, _pos.Y + fontBorder, 0, _colorShadow, _fontScale.Y, space);

                    }
                    canvas.Batcher.GetPackedTextureBatcher().DrawFont(_font, Text, _pos.X, _pos.Y, 0, _colorFinal, _fontScale.Y, space);
                }
                if (Cut)
                {
                    canvas.Batcher.EndDraw();
                    //推裁剪
                    canvas.Batcher.PopLimitRect();
                }
            }
            base.OnRender(canvas);
        }
    }
}